package com.xhwl.data.pojo.space;

import com.xhwl.common.dto.cent.sdata.RoomDTO;
import com.xhwl.common.enums.RoomOrientationEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Room;
import com.xhwl.common.utils.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 16:55 2023/4/3
 */
public class RoomCheck {

    public static ResultJson check(Room room){
        if (null != room.getOrientation()) {
            List<RoomOrientationEnum> roomOrientationEnums = Arrays.asList(RoomOrientationEnum.values());
            if (!roomOrientationEnums.contains(room.getOrientation())) {
                return ResultJson.fail("朝向输入错误");
            }
        }
        if (StringUtils.isEmpty(room.getName()) || room.getName().length() > 32) {
            return ResultJson.fail("名称输入错误");
        }
        if (StringUtils.isNotEmpty(room.getCode()) && room.getCode().length() > 36) {
            return ResultJson.fail("编码输入错误");
        }
        if (null == room.getParentId() || room.getParentId().toString().length() > 11) {
            return ResultJson.fail("上级信息错误");
        }

        if (null != room.getArea()) {
            BigDecimal bigDecimal = new BigDecimal(room.getArea());
            bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP);
            if(!isValidDecimal(String.valueOf(bigDecimal))){
                return ResultJson.fail("面积错误");
            }
        }
        if (null != room.getInsideSpace()) {
            BigDecimal bigDecimal1 = new BigDecimal(room.getInsideSpace());
            bigDecimal1 = bigDecimal1.setScale(2, RoundingMode.HALF_UP);
            if(!isValidDecimal(String.valueOf(bigDecimal1))){
                return ResultJson.fail("套内面积错误");
            }
        }
        if (null != room.getUsableArea()) {
            BigDecimal bigDecimal2 = new BigDecimal(room.getUsableArea());
            bigDecimal2 = bigDecimal2.setScale(2, RoundingMode.HALF_UP);
            if(!isValidDecimal(String.valueOf(bigDecimal2))){
                return ResultJson.fail("使用面积错误");
            }
        }
        if (null != room.getResidentialPoolArea()) {
            BigDecimal bigDecimal3 = new BigDecimal(room.getResidentialPoolArea());
            bigDecimal3 = bigDecimal3.setScale(2, RoundingMode.HALF_UP);
            if(!isValidDecimal(String.valueOf(bigDecimal3))){
                return ResultJson.fail("公摊面积错误");
            }
        }
        return ResultJson.success();
    }

    public static boolean isValidDecimal(String str) {
        // 匹配小数点前最多13位数字，小数点后最多2位数字
        String pattern = "^\\d{1,13}(\\.\\d{1,2})?$";
        return str.matches(pattern);
    }

}
